package com.vvlive.caibao.smartcity.ui;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.Person;
import com.vvlive.caibao.smartcity.data.BaseData;
import com.vvlive.caibao.smartcity.utils.ImageUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.ui.BindView;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;
import smartcity.vvlive.caibao.com.smartc.R;

import static com.vvlive.caibao.smartcity.view.CommonActionBar.OnMoreListener;

/**
 * Created by zhj on 2015/7/3.
 */
public class GroupsActivity extends BaseActivity implements OnMoreListener {

    @BindView(id = R.id.group_members_container)
    private LinearLayout mGroups;

    private List<String> mIds;
    private List<Person> mPersons;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_groups;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("选择群组成员");
        actionBar.setOnMoreListener(this);
        mIds = new ArrayList<String>();
        mPersons = BaseData.getPersonData();
        loadMembers();
    }

    private void loadMembers() {
        for (int i = 0; i < mPersons.size(); i++) {
            View item = getLayoutInflater().inflate(R.layout.item_groups_list, null, false);
            ImageView avatar = (ImageView) item.findViewById(R.id.group_member_avatar);
            TextView name = (TextView) item.findViewById(R.id.group_member_name);
            Person person = mPersons.get(i);

            ImageUtil.loadImage(this, person.getPortrait(), avatar, 0);
            name.setText(person.getName());

            mGroups.addView(item);
        }
    }

    @Override
    public void onMore(View v) {
        mIds.clear();

        for (int i = 0; i < mGroups.getChildCount() ; i++){
           if( ((CheckBox) mGroups.getChildAt(i).findViewById(R.id.group_member_select)).isChecked()){
               mIds.add(mPersons.get(i).getNumber());
           }

        }
        if(mIds.size() == 0 || mIds.isEmpty()){
            toast("没有选择任何好友不能创建群组！");
            return;
        }
        String discussionName = "";
        for (String id : mIds){
            discussionName += id +", ";
        }

        RongIM.getInstance().createDiscussionChat(this, mIds, discussionName);
    }
}
